﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using MvvmFx.Common.ViewModels.Behaviors.Validation;

namespace MvvmFx.Common.ViewModels
{
    public abstract partial class ViewModelBase
    {
        #region [ Notify PropertyChanged ]

        /// <summary>
        /// On the specified property changing invoke any handlers attached.
        /// </summary>
        /// <param name="propertyName">The name of the property that changed.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            VerifyPropertyName(propertyName);

            var args = new PropertyChangedEventArgs(propertyName);
            try
            {
                if (PropertyChanged != null)
                    this.PropertyChanged(this, args);
            }
            catch (ValidationException ex)
            {
                throw new Exception(ex.Message);
            }
            catch (Exception)
            {
                //Suppress errors in databound controls from affecting the others
                //
                Debug.WriteLine("Exception occurred in property changed handler.");
            }
        }

        #endregion
    }
}
